package com.hl.service.serviceImpl;

import com.hl.domain.Course.CourseUser;
import com.hl.domain.PsychologicalTopic.PsychologicalTopic;
import com.hl.domain.PsychologicalTopic.PsychologicalTopicReply;
import com.hl.mapper.CourseMapper;
import com.hl.mapper.PsychologicalTopicMapper;
import com.hl.service.PsychologicalTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PsychologicalTopicServiceImpl implements PsychologicalTopicService {

    @Autowired
    private PsychologicalTopicMapper psychologicalTopicMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<PsychologicalTopic> selectAllTopic() {
        List<PsychologicalTopic> topics = psychologicalTopicMapper.selectAllTopic();
        for (PsychologicalTopic topic : topics) {
            if (topic!=null){
                List<PsychologicalTopicReply> PsychologicalTopicReply = selectAllReplyByTopic(Math.toIntExact(topic.getId()));
                CourseUser user = courseMapper.selectCourseUserById(Math.toIntExact(topic.getUserId()));
                if (PsychologicalTopicReply !=null){
                    topic.setReplies(PsychologicalTopicReply);
                }
                if (user !=null){
                    topic.setCourseUser(user);
                }
            }
        }
        return topics;
    }

    @Override
    public List<PsychologicalTopicReply> selectAllReplyByTopic(int i) {
        List<PsychologicalTopicReply> replies = psychologicalTopicMapper.selectAllReplyByTopicId(i);
        for (PsychologicalTopicReply reply : replies) {
            if (reply!=null){
                CourseUser user = courseMapper.selectCourseUserById(Math.toIntExact(reply.getUserId()));
                CourseUser replytouser = courseMapper.selectCourseUserById(Math.toIntExact(reply.getReplytoId()));
                reply.setUser(user);
                reply.setReplytouser(replytouser);
            }
        }
        return replies;

    }

    @Override
    public int addReply(PsychologicalTopicReply reply) {
        return psychologicalTopicMapper.addReply(reply);
    }

    @Override
    public int addTopic(PsychologicalTopic topic) {
        return psychologicalTopicMapper.addtopic(topic);
    }

    @Override
    public int deleteTopic(Long i) {
        return psychologicalTopicMapper.deleteTopic(i);
    }

    @Override
    public int deleteTopicReply(Long i) {
        return psychologicalTopicMapper.deleteTopicReply(i);
    }
}
